<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 2018/7/23
 * Time: 18:29
 */
namespace app\home\controller;

use app\common\controller\Frontend;
use think\Db;

class Scoutechain extends Frontend
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = '*';
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    protected $layout = '';
    protected $res = [];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 球探链首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        $award = Db::name('award_plan')->where('status','normal')->order('id')->select();
        $config = Db::name('currency_config')->where('status','normal')->find();
        $start_Md = date('M.d',$config['starttime']);
        $end_Md = date('M.d',$config['endtime']);
        $start_Hi = date('H:i a',$config['starttime']);
        $end_Hi = date('H:i a',$config['endtime']);
        $config['start_Md'] = $start_Md;
        $config['end_Md'] = $end_Md;
        $config['start_Hi'] = $start_Hi;
        $config['end_Hi'] = $end_Hi;
        $val = json_encode($config);
        $this->assign('list',$config);
        $this->assign('val',$val);
        $this->assign('award',$award);
        //true:手机端;false:游览器
        $flag = isMobile();
        if( $flag ) {
            return $this->view->fetch('scoute/index_mobile');
        } else {
            return $this->view->fetch('scoute/index');
        }
    }

    /**
     * 兑换提交
     * @return \think\response\Json
     */
    public function assetExchange(){
        if($this->request->isAjax()){
            $time  = time();
            $data = input();
            $data['refreshtime'] = $time;
            $data['completetime'] = $time;

            //cam兑换cts
            if($data['type']==3){


                /**
                 *
                 * ……
                 * 判断cam上限依据
                 */


                $res = array(
                    'status' => false,
                    'msg'    => '兑换数量有限'
                );
                return json($res);
            }
            $insert = Db::name('asset_exchange')->insert($data);
            if($insert){
                $res = array(
                    'status' => true,
                    'msg'    => '兑换成功'
                );
                return json($res);
            }else{
                $res = array(
                    'status' => false,
                    'msg'    => '兑换失败'
                );
                return json($res);
            }
        }else{
            $res = array(
                'status' => false,
                'msg'    => '非法请求'
            );
            return json($res);
        }

    }

    /*
     * 博客
     */
    public function blog()
    {
        $flag = isMobile();
        if( $flag ) {
            return $this->view->fetch('scoute/blog_mobile');
        } else {
            return $this->view->fetch('scoute/blog');
        }
    }
}